package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JEditorPane;
import javax.swing.JComboBox;
import javax.swing.JButton;

import CONTROLLERS.DonThuocConTrollers;
import ModelEntityFW.DonThuocEntity;
import ModelEntityFW.UserEntitys;
import Ultils.SQLConnections;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class THEMSUADONTHUOC extends JFrame {

	private JPanel contentPane;
	private JTextField thuocID;
	private JTextField SoLuong;
	private JTextField CachDung;
	private JComboBox CTBA;
	private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private DonThuocConTrollers objcontroller;
	private DonThuocEntity m_objEntity;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public THEMSUADONTHUOC(Connection objConnection, UserEntitys objSecurity,
			DonThuocEntity objDonThuocEntity) {
		m_objConnection = objConnection;
		m_objSecurityContext = objSecurity;
		objcontroller = new DonThuocConTrollers(m_objConnection, m_objSecurityContext);
		m_objEntity = objDonThuocEntity;

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblDo = new JLabel("ThuocID:");
		lblDo.setBounds(25, 21, 73, 14);
		contentPane.add(lblDo);

		thuocID = new JTextField();
		thuocID.setBounds(128, 18, 86, 20);
		contentPane.add(thuocID);
		thuocID.setColumns(10);

		JLabel lblCtba = new JLabel("CTBA :");
		lblCtba.setBounds(25, 57, 46, 14);
		contentPane.add(lblCtba);

		JLabel lblS = new JLabel("SoLuong :");
		lblS.setBounds(25, 101, 73, 14);
		contentPane.add(lblS);

		SoLuong = new JTextField();
		SoLuong.setBounds(128, 98, 86, 20);
		contentPane.add(SoLuong);
		SoLuong.setColumns(10);

		JLabel lblCachDung = new JLabel("Cach Dung:");
		lblCachDung.setBounds(25, 147, 73, 14);
		contentPane.add(lblCachDung);

		CachDung = new JTextField();
		CachDung.setBounds(126, 146, 249, 38);
		contentPane.add(CachDung);
		CachDung.setColumns(10);

		Vector<String> CTBA_ID=GetDataJCB();
		
		CTBA = new JComboBox(CTBA_ID);
		CTBA.setBounds(128, 54, 86, 20);
		contentPane.add(CTBA);

		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(m_objEntity.isbFlag()){
					m_objEntity.setThuocID(Integer.parseInt(thuocID.getText()));
					m_objEntity.setCTBA_ID(Integer.parseInt(""+CTBA.getSelectedItem()));
					m_objEntity.setCachDung(CachDung.getText());
					m_objEntity.setSoLuong(Integer.parseInt(SoLuong.getText()));
					
					boolean bResult = false;
					try {
						bResult = objcontroller.ExecOfUpdateElement(m_objEntity);
					} catch (Exception e2) {
						// TODO: handle exception
						JOptionPane.showMessageDialog(getParent(), "Loi: "+e2.getMessage(), "Loi", JOptionPane.ERROR);
					}
					
					if(bResult){
						JOptionPane.showMessageDialog(getParent(), "Cap nhat thanh cong", "thong Bao", JOptionPane.INFORMATION_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(getParent(), "Cap nhat that bai", "Loi", JOptionPane.ERROR);
					}
				}else{
					m_objEntity.setThuocID(Integer.parseInt(thuocID.getText()));
					m_objEntity.setCTBA_ID(Integer.parseInt(""+CTBA.getSelectedItem()));
					m_objEntity.setCachDung(CachDung.getText());
					m_objEntity.setSoLuong(Integer.parseInt(SoLuong.getText()));
					
					boolean bResult = false;
					try {
						bResult = objcontroller.ExecOfInsertElement(m_objEntity);
					} catch (Exception e2) {
						// TODO: handle exception
						JOptionPane.showMessageDialog(getParent(), "Loi: "+e2.getMessage(), "Loi", JOptionPane.ERROR);
					}
					
					if(bResult){
						JOptionPane.showMessageDialog(getParent(), "Them moi thanh cong", "thong Bao", JOptionPane.INFORMATION_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(getParent(), "Them moi that bai", "Loi", JOptionPane.ERROR);
					}
				}
			}
		});
		btnNewButton.setBounds(128, 214, 116, 23);
		contentPane.add(btnNewButton);

		JButton btnThoat = new JButton("Thoat");
		btnThoat.setBounds(286, 214, 89, 23);
		contentPane.add(btnThoat);

		if (m_objEntity.isbFlag()) {
			BinldingControl(m_objEntity);
			thuocID.setEditable(false);
			btnNewButton.setText("Cap nhat");
		} else {
			btnNewButton.setText("Them moi");
		}
	}

	public Vector<String> GetDataJCB() {
		Vector<String> vector = new Vector<String>();
		CallableStatement objCmst = null;
		ResultSet objRs = null;
		try {
			if (!m_objConnection.isClosed()) {
				objCmst = m_objConnection.prepareCall("{CALL SP_GET_CTBA()}");
				objRs = objCmst.executeQuery();
			}
			while (objRs.next()) {
				vector.addElement(objRs.getString("CTBA_ID"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		
		return vector;
	}

	public void BinldingControl(DonThuocEntity obj) {
		thuocID.setText("" + obj.getThuocID());
		CTBA.setSelectedItem(""+obj.getCTBA_ID());
		SoLuong.setText("" + obj.getSoLuong());
		CachDung.setText(obj.getCachDung());
	}
}
